package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.ApkDAO;
import com.pojo.Apk;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApkService extends ServiceImpl<ApkDAO, Apk> {

    @Autowired
    private ApkDAO apkDAO;

    /**
     * 根据apk名称查询版本
     * @param apkName
     * @return
     */
    public Apk getCurrentVersion(String apkName){
        QueryWrapper query = new QueryWrapper();
        query.eq("publicity",1);
        query.eq("status",1);
        if (StringUtil.isNotNull(apkName)) {
            query.eq("apk_name",apkName);
        }
        query.orderByDesc("create_time");
        List<Apk> apkList = list(query);
        if (apkList.size() > 0) {
            return apkList.get(0);
        }
        return null;
    }

    /**
     * 查询apk信息
     * /getApkListByElife
     * @return
     */
    public IPage getApkListByElife(int page, int limit, String apkName, String carType, String carGs) {
        Page iPage = new Page(page, limit);
        QueryWrapper<Apk> query = new QueryWrapper();

        if(StringUtil.isNotNull(apkName)) {
            query.like("apk_name" , apkName) ;
        }
        return apkDAO.selectPage(iPage, query);
    }

    public int countApk(String versions,String apkName){
        return apkDAO.selectList(new QueryWrapper<Apk>().eq("versions",versions).eq("apk_name",apkName)).size();
    }
    public int saveApk(Apk apk){
        return apkDAO.insert(apk);
    }
}
